package com.lucenly.card.activity;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lucenly.card.R;
import com.lucenly.card.base.BaseActivity;
import com.lucenly.card.constants.UrlDefend;
import com.siberiadante.toastutils.ToastUtil;
import com.vise.xsnow.dialog.PromptDialog;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import butterknife.BindView;

public class CodeActivity extends BaseActivity {
//    @BindView(R.id.captCha)
//    Captcha captCha;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
//    @BindView(R.id.swipecaptchaview)
//    SwipeCaptchaView swipecaptchaview;
//    @BindView(R.id.sb_progress)
//    SeekBar seekbar;
    @BindView(R.id.webView)
    WebView webview;
    PromptDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code);
    }

    @Override
    protected void initView() {
        ll_back.setVisibility(View.VISIBLE);
        tv_title.setText("验证");
        dialog  = new PromptDialog(this);
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.banner_bg2x);
        //设置图片的资源
//        swipecaptchaview.setImageBitmap(bitmap);
        //创建截图的阴影
//        swipecaptchaview.createCaptcha();

        WebSettings settings = webview.getSettings();
        //配置支持domstorage
        settings.setDomStorageEnabled(true);//启用或禁用DOM缓存
        //settings.setBuiltInZoomControls(false);//是否显示缩放按钮，默认false
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        webview.loadUrl("file:///android_asset/verify-master/index.html");
    }

    @Override
    protected void bindEvent() {
        C(ll_back);
//        captCha.setCaptchaListener(new Captcha.CaptchaListener() {
//            @Override
//            public String onAccess(long time) {
//                return "验证通过,耗时"+time+"毫秒";
//            }
//
//            @Override
//            public String onFailed(int failedCount) {
//                captCha.reset(false);
//                return "验证失败,已失败"+failedCount+"次";
//            }
//
//            @Override
//            public String onMaxFailed() {
//                captCha.reset(false);
//                return "验证失败次数过多,请稍后再试";
//            }
//        });
        //设置回调监听
//        swipecaptchaview.setOnCaptchaMatchCallback(new SwipeCaptchaView.OnCaptchaMatchCallback() {
//            @Override
//            public void matchSuccess(SwipeCaptchaView swipeCaptchaView) {
//                Toast.makeText(CodeActivity.this,"恭喜你，验证成功。可以进行尽情的玩耍了",Toast.LENGTH_SHORT).show();
//                //设置拖动
//                seekbar.setEnabled(false);
//            }
//
//            @Override
//            public void matchFailed(SwipeCaptchaView swipeCaptchaView) {
//                Toast.makeText(CodeActivity.this,"完成了80%的比对，还差一点就成功了",Toast.LENGTH_SHORT).show();
//                //设置拖动
//                swipeCaptchaView.resetCaptcha();
//                seekbar.setProgress(0);
//            }
//        });

//        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//                swipecaptchaview.setCurrentSwipeValue(i);
//
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//                //设置进度条的最大值
//                seekBar.setMax(swipecaptchaview.getMaxSwipeValue());
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//                //进行比对截图是否一样
//                swipecaptchaview.matchCaptcha();
//            }
//        });

        webview.setWebChromeClient(new WebChromeClient() {

            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     final JsResult result) {
                if(message.equals("success")){
                    dk();
                }
                result.cancel();
                return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url,
                                       String message, final JsResult result) {
                // TODO Auto-generated method stub
                return true;
            }
        });
        webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url.toString());
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //这段js函数的功能就是注册监听，遍历所有的img标签，并添加onClick函数，函数的功能是在图片点击的时候调用本地java接口并传递url过去
            }
            @Override
            public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                sslErrorHandler.proceed();
            }

            @Override
            public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
//                view1.setErrorText("网络连接缓慢，暂时无法加载!").showError();
            }
        });
    }

    @Override
    protected void initData() {

    }
    /**
     * 打卡
     */
    private void dk() {
        ViseHttp.POST(UrlDefend.DAKA)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        dialog.dismiss();
                        ToastUtil.showSingletonShort("打卡成功!");
                        finish();
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        dialog.dismiss();
                        ToastUtil.showSingletonShort(errMsg);
                        webview.reload();
                    }
                });
    }
    @Override
    protected void processClick(View view) {
        switch (view.getId()){
            case R.id.ll_back:
                finish();
                break;
        }
    }
}
